package com.kkw.shoppingmall.order.web;

import com.alipay.api.AlipayApiException;

import com.kkw.shoppingmall.order.config.AlipayTemplate;
import com.kkw.shoppingmall.order.service.OrderService;
import com.kkw.shoppingmall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 去支付宝的付款页面
 */
@Controller
public class PayWebController {

    @Autowired
    private AlipayTemplate alipayTemplate;

    @Autowired
    private OrderService orderService;

    /**
     * text/html明确告诉浏览器 返回的是一个页面
     * 支付成功以后，我们要跳转到用户的订单列表页面
     * @param orderSn
     * @return
     * @throws AlipayApiException
     */
    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String aliPayOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        System.out.println("接收到订单信息orderSn："+orderSn);
        PayVo payVo = orderService.getOrderPay(orderSn);
        //返回的是一个页面。将此页面直接交给浏览器就行了
        String pay = alipayTemplate.pay(payVo);
        return pay;
    }


}
